package com.project.controller;

import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService service;
    /**登录 */
    @RequestMapping("login")
    public String login(String userName, String pwd, HttpServletRequest request){
        UserBean userBean = service.login(userName,pwd);
        if(userBean == null){
            return "no";
        }else {
            request.getSession().setAttribute("loginUser",userBean);
            return "ok";
        }
    }

    /**注册 */
    @RequestMapping("register")
    public String register(UserBean userBean){
        UserBean user =  service.findByUser(userBean.getName());
        if(user==null){
            service.add(userBean);
            return "ok";
        }else {
            return "no";
        }
    }

    /**获取当前用户对象 */
    @RequestMapping("getLoginUser")
    public UserBean getLoginUser(HttpServletRequest req){
        return (UserBean )req.getSession().getAttribute("loginUser");
    }
}
